package com.ziq.base.view.webview

import android.app.Activity
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.view.View
import android.webkit.ValueCallback
import android.webkit.WebChromeClient
import android.webkit.WebView
import androidx.activity.result.contract.ActivityResultContracts
import androidx.core.view.isVisible
import com.blankj.utilcode.util.GsonUtils
import com.github.lzyzsd.jsbridge.CallBackFunction
import com.ziq.base.R
import com.ziq.base.base.BaseFragment


open class BaseWebViewFragment(contentLayoutId: Int) : BaseFragment(contentLayoutId) {

    companion object {
        const val TARGET_URL = "target_url"
        const val IS_SHOW_TOOLBAR = "isShowToolbar"
    }

    var mToolBar : View? = null
    var mWebView : BaseWebView? = null

    var mInitUrl : String = ""
    var mIsShowToolbar : Boolean = true

    private var mUploadCallbackAboveL: ValueCallback<Array<Uri>>? = null


    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initView()
        setUpView()
    }

    open fun initView(){
        mToolBar = view?.findViewById(R.id.title_bar)
        mWebView = view?.findViewById(R.id.base_web_view)
        val backBtn : View? = view?.findViewById(R.id.back)
        backBtn?.setOnClickListener {
            activity?.finish()
        }
    }


    open fun setUpView(){
        arguments?.let {bundle ->
            mIsShowToolbar = bundle.getBoolean(IS_SHOW_TOOLBAR, true)
            mInitUrl = bundle.getString(TARGET_URL)?:""
        }
        mWebView?.jsHandler = object : BaseWebView.JsHandler {
            override fun receiveFromJs(actionInfo: BaseWebView.ActionInfo, callback : CallBackFunction) {
                when(actionInfo.method){
                    "closeWeb" -> {
                        callback.onCallBack(GsonUtils.toJson(BaseWebView.ActionResult()))
                        activity?.finish()
                    }
                }
            }
        }
        mWebView?.webChromeClient = object : WebChromeClient() {
            override fun onShowFileChooser(
                webView: WebView,
                filePathCallback: ValueCallback<Array<Uri>>,
                fileChooserParams: FileChooserParams?
            ): Boolean {
                if (mUploadCallbackAboveL != null) {
                    mUploadCallbackAboveL?.onReceiveValue(null)
                    mUploadCallbackAboveL = null
                }
                mUploadCallbackAboveL = filePathCallback
                openFileChooseProcess()
                return true
            }
        }
        mToolBar?.isVisible = mIsShowToolbar
        mWebView?.loadUrl(mInitUrl)
    }

    fun openFileChooseProcess() {
        val i = Intent(Intent.ACTION_GET_CONTENT)
        i.addCategory(Intent.CATEGORY_OPENABLE)
        i.type = "image/*"
        uploadFileLauncher.launch(i)
    }

    private val uploadFileLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()){ activityResult ->
        if(activityResult.resultCode == Activity.RESULT_OK){
            if (mUploadCallbackAboveL != null) {
                val uri = activityResult.data?.data
                if(uri != null){
                    mUploadCallbackAboveL?.onReceiveValue(arrayOf(uri))
                } else {
                    mUploadCallbackAboveL?.onReceiveValue(null)
                }

                mUploadCallbackAboveL = null
            }
        } else {
            if (mUploadCallbackAboveL != null) {
                mUploadCallbackAboveL?.onReceiveValue(null)
                mUploadCallbackAboveL = null
            }
        }
    }

}